﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;
using WXYW.Model;

namespace WXYW
{
    public partial class ProviderDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProviderService providerService = new ProviderService();
                Provider providerEntity = providerService.GetProviderByID(Convert.ToInt32(Request.QueryString["ID"]));

                BindBasicInfo(providerEntity);
                BindPriceDocumentGridView();
                BindContractDocumentGridView();
                BindProductMaterialGridView();
                BindTechnologyMaterialGridView();
                BindRepairMaterialGridView();
                BindSparePartGridView();
                BindAccessoryListGridView();
            }
        }

        private void BindBasicInfo(Provider providerEntity)
        {            
            codeTxtBox.Text = providerEntity.ProviderCode;
            nameTxtBox.Text = providerEntity.ProviderName;

            ProductClassService classService = new ProductClassService();
            productTypeDropdownList.DataSource = classService.GetAllClassWithSelectOne();
            productTypeDropdownList.DataTextField = "Name";
            productTypeDropdownList.DataValueField = "ID";
            if (!providerEntity.ProductClassReference.IsLoaded)
            {
                providerEntity.ProductClassReference.Load();
            }
            productTypeDropdownList.SelectedValue = providerEntity.ProductClass.ID.ToString();
            productTypeDropdownList.DataBind();
            relationTxtBox.Text = providerEntity.Relation;
            cityTxtBox.Text = providerEntity.City;
            postcodeTxtBox.Text = providerEntity.Postcode;
            addressTxtBox.Text = providerEntity.Address;
            contact1TxtBox.Text = providerEntity.Contact1Name;            
            contact2TxtBox.Text = providerEntity.Contact2Name;
            phone1TxtBox.Text = providerEntity.Contact1Phone;
            phone2TxtBox.Text = providerEntity.Contact2Phone;
            mobilePhone1TxtBox.Text = providerEntity.Contact1MobilePhone;
            mobilePhone2TxtBox.Text = providerEntity.Contact2MobilePhone;
            fax1TxtBox.Text = providerEntity.Contact1Fax;
            fax2TxtBox.Text = providerEntity.Contact2Fax;
            email1TxtBox.Text = providerEntity.Contact1Email;
            email2TxtBox.Text = providerEntity.Contact2Email;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.UpdateProvide(Convert.ToInt32(Request.QueryString["ID"]), codeTxtBox.Text, nameTxtBox.Text, Convert.ToInt32(productTypeDropdownList.SelectedValue), relationTxtBox.Text,
                    contact1TxtBox.Text, phone1TxtBox.Text, mobilePhone1TxtBox.Text, fax1TxtBox.Text, email1TxtBox.Text, contact2TxtBox.Text,
                    phone2TxtBox.Text, mobilePhone2TxtBox.Text, fax2TxtBox.Text, email2TxtBox.Text, postcodeTxtBox.Text, cityTxtBox.Text, addressTxtBox.Text,
                    ContextService.CurrentAccount);
                messageLbl.Text = "已成功更新代理商信息。";
            }
            catch (Exception ex)
            {
                messageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void UploadPriceDocumentButton_Click(object sender, EventArgs e)
        {
            try
            {
                string fileType = string.Empty;
                string fileName = string.Empty;
                string fileNameOnServer = string.Empty;
                if (PriceFileUpload.HasFile)
                {
                    fileType = PriceFileUpload.FileName.Substring(PriceFileUpload.FileName.LastIndexOf("."));
                    fileName = PriceFileUpload.FileName;
                    fileNameOnServer = Guid.NewGuid() + fileType;
                    PriceFileUpload.SaveAs(Server.MapPath("Document/" + fileNameOnServer));
                }

                ProviderService providerService = new ProviderService();
                providerService.AddDocument(Convert.ToInt32(Request.QueryString["ID"]), fileName, fileType, fileNameOnServer, "报价单", ContextService.CurrentAccount);
                PriceMessageLbl.Text = "报价单已成功上传。";
                BindPriceDocumentGridView();
            }
            catch (Exception ex)
            {
                PriceMessageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void ProviderInfoTabContainer_ActiveTabChanged(object sender, EventArgs e)
        {
            switch (ProviderInfoTabContainer.ActiveTabIndex)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    BindPriceDocumentGridView();
                    PriceMessageLbl.Text = string.Empty;
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default:
                    break;
            }
        }

        private void BindPriceDocumentGridView()
        {
            ProviderService providerService = new ProviderService();
            PriceGridView.DataSource = providerService.GetProviderDocument(Convert.ToInt32(Request.QueryString["ID"]), "报价单");
            PriceGridView.DataBind();
        }

        private void BindContractDocumentGridView()
        {
            ProviderService providerService = new ProviderService();
            ContractGridView.DataSource = providerService.GetProviderDocument(Convert.ToInt32(Request.QueryString["ID"]), "协议");
            ContractGridView.DataBind();
        }

        private void BindProductMaterialGridView()
        {
            ProviderService providerService = new ProviderService();
            ProductMaterialGridView.DataSource = providerService.GetProviderDocument(Convert.ToInt32(Request.QueryString["ID"]), "产品资料");
            ProductMaterialGridView.DataBind();
        }

        protected void PriceGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                HyperLink openLnk = e.Row.FindControl("OpenLnk") as HyperLink;
                openLnk.NavigateUrl = "~/Document/" + ((ProviderDocumentModel)e.Row.DataItem).NameOnServer;
            }
        }

        protected void PriceGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.DeleteDocument(Convert.ToInt32(e.CommandArgument));
                BindPriceDocumentGridView();
                PriceMessageLbl.Text = "文件已被删除。";
            }
            catch (Exception ex)
            {
                PriceMessageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void ContractGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.DeleteDocument(Convert.ToInt32(e.CommandArgument));
                BindContractDocumentGridView();
                ContractMessageLbl.Text = "文件已被删除。";
            }
            catch (Exception ex)
            {
                ContractMessageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void ContractGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                HyperLink openLnk = e.Row.FindControl("OpenLnk") as HyperLink;
                openLnk.NavigateUrl = "~/Document/" + ((ProviderDocumentModel)e.Row.DataItem).NameOnServer;
            }
        }

        protected void UploadContractButton_Click(object sender, EventArgs e)
        {
            try
            {
                string fileType = string.Empty;
                string fileName = string.Empty;
                string fileNameOnServer = string.Empty;
                if (ContractFileUpload.HasFile)
                {
                    fileType = ContractFileUpload.FileName.Substring(ContractFileUpload.FileName.LastIndexOf("."));
                    fileName = ContractFileUpload.FileName;
                    fileNameOnServer = Guid.NewGuid() + fileType;
                    ContractFileUpload.SaveAs(Server.MapPath("Document/" + fileNameOnServer));
                }

                ProviderService providerService = new ProviderService();
                providerService.AddDocument(Convert.ToInt32(Request.QueryString["ID"]), fileName, fileType, fileNameOnServer, "协议", ContextService.CurrentAccount);
                ContractMessageLbl.Text = "协议文件已成功上传。";
                BindContractDocumentGridView();
            }
            catch (Exception ex)
            {
                ContractMessageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void UploadProductMaterialButton_Click(object sender, EventArgs e)
        {
            try
            {
                string fileType = string.Empty;
                string fileName = string.Empty;
                string fileNameOnServer = string.Empty;

                if (ProductMaterialFileUpload.HasFile)
                {
                    fileType = ProductMaterialFileUpload.FileName.Substring(ProductMaterialFileUpload.FileName.LastIndexOf("."));
                    fileName = ProductMaterialFileUpload.FileName;
                    fileNameOnServer = Guid.NewGuid() + fileType;
                    ProductMaterialFileUpload.SaveAs(Server.MapPath("Document/" + fileNameOnServer));
                }

                ProviderService providerService = new ProviderService();
                providerService.AddDocument(Convert.ToInt32(Request.QueryString["ID"]), fileName, fileType, fileNameOnServer, "产品资料", ContextService.CurrentAccount);
                ProductMaterialLabel.Text = "产品资料已成功上传。";
                BindProductMaterialGridView();
            }
            catch (Exception ex)
            {
                ProductMaterialLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void ProductMaterialGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                HyperLink openLnk = e.Row.FindControl("OpenLnk") as HyperLink;
                openLnk.NavigateUrl = "~/Document/" + ((ProviderDocumentModel)e.Row.DataItem).NameOnServer;
            }
        }

        protected void ProductMaterialGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.DeleteDocument(Convert.ToInt32(e.CommandArgument));
                BindProductMaterialGridView();
                ProductMaterialLabel.Text = "文件已被删除。";
            }
            catch (Exception ex)
            {
                ProductMaterialLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void UploadTechnologyMaterialButton_Click(object sender, EventArgs e)
        {
            try
            {
                string fileType = string.Empty;
                string fileName = string.Empty;
                string fileNameOnServer = string.Empty;

                if (TechnologyMaterialFileUpload.HasFile)
                {
                    fileType = TechnologyMaterialFileUpload.FileName.Substring(TechnologyMaterialFileUpload.FileName.LastIndexOf("."));
                    fileName = TechnologyMaterialFileUpload.FileName;
                    fileNameOnServer = Guid.NewGuid() + fileType;
                    TechnologyMaterialFileUpload.SaveAs(Server.MapPath("Document/" + fileNameOnServer));
                }

                ProviderService providerService = new ProviderService();
                providerService.AddDocument(Convert.ToInt32(Request.QueryString["ID"]), fileName, fileType, fileNameOnServer, "技术资料", ContextService.CurrentAccount);
                ProductMaterialLabel.Text = "技术资料已成功上传。";
                BindTechnologyMaterialGridView();
            }
            catch (Exception ex)
            {
                ProductMaterialLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        private void BindTechnologyMaterialGridView()
        {
            ProviderService providerService = new ProviderService();
            TechnologyGridView.DataSource = providerService.GetProviderDocument(Convert.ToInt32(Request.QueryString["ID"]), "技术资料");
            TechnologyGridView.DataBind();
        }

        protected void TechnologyGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.DeleteDocument(Convert.ToInt32(e.CommandArgument));
                BindTechnologyMaterialGridView();
                TechnologyMaterialMessageLbl.Text = "文件已被删除。";
            }
            catch (Exception ex)
            {
                TechnologyMaterialMessageLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void TechnologyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                HyperLink openLnk = e.Row.FindControl("OpenLnk") as HyperLink;
                openLnk.NavigateUrl = "~/Document/" + ((ProviderDocumentModel)e.Row.DataItem).NameOnServer;
            }
        }

        private void BindRepairMaterialGridView()
        {
            ProviderService providerService = new ProviderService();
            RepaireMaterialGridView.DataSource = providerService.GetProviderDocument(Convert.ToInt32(Request.QueryString["ID"]), "维修资料");
            RepaireMaterialGridView.DataBind();
        }

        protected void UploadRepaireMaterialButton_Click(object sender, EventArgs e)
        {
            try
            {
                string fileType = string.Empty;
                string fileName = string.Empty;
                string fileNameOnServer = string.Empty;


                if (RepaireMaterialFileUpload.HasFile)
                {
                    fileType = RepaireMaterialFileUpload.FileName.Substring(RepaireMaterialFileUpload.FileName.LastIndexOf("."));
                    fileName = RepaireMaterialFileUpload.FileName;
                    fileNameOnServer = Guid.NewGuid() + fileType;
                    RepaireMaterialFileUpload.SaveAs(Server.MapPath("Document/" + fileNameOnServer));
                }

                ProviderService providerService = new ProviderService();
                providerService.AddDocument(Convert.ToInt32(Request.QueryString["ID"]), fileName, fileType, fileNameOnServer, "维修资料", ContextService.CurrentAccount);
                ProductMaterialLabel.Text = "维修资料已成功上传。";
                BindRepairMaterialGridView();
            }
            catch (Exception ex)
            {
                ProductMaterialLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void RepaireMaterialGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.DeleteDocument(Convert.ToInt32(e.CommandArgument));
                BindRepairMaterialGridView();
                RepairMaterialLabel.Text = "文件已被删除。";
                
            }
            catch (Exception ex)
            {
                RepairMaterialLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void RepaireMaterialGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                HyperLink openLnk = e.Row.FindControl("OpenLnk") as HyperLink;
                openLnk.NavigateUrl = "~/Document/" + ((ProviderDocumentModel)e.Row.DataItem).NameOnServer;
            }
        }

        protected void NewSparePartButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.AddSparePart(Convert.ToInt32(Request.QueryString["ID"]), ModelNOTxtBox.Text, VersionNOTextBox.Text,
                    Convert.ToInt32(CountTextBox.Text), ContextService.CurrentAccount);
                NewSparePartMsgLbl.Text = "备件已经成功添加，请继续添加下一条。";
                ClearSparePartForm();
                BindSparePartGridView();
            }
            catch (Exception ex)
            {
                NewSparePartMsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }

            NewSparePartPopupExtender.Show();
        }

        private void ClearSparePartForm()
        {
            ModelNOTxtBox.Text = string.Empty;
            VersionNOTextBox.Text = string.Empty;
            CountTextBox.Text = string.Empty;
        }

        private void BindSparePartGridView()
        {
            ProviderService providerService = new ProviderService();
            SparePartGridView.DataSource = providerService.GetSparePartOfProvider(Convert.ToInt32(Request.QueryString["ID"]));
            SparePartGridView.DataBind();
        }

        protected void SparePartGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ProviderService providerService = new ProviderService();
            if (e.CommandName.Equals("BackSparePart"))
            {
                ProviderSparePart providerSparePartEntity = providerService.GetSparePartByID(Convert.ToInt32(e.CommandArgument));
                BackSparePartModelNOTextBox.Text = providerSparePartEntity.ModelNumber;
                BackSparePartVersionNOTextBox.Text = providerSparePartEntity.VersionNumber;
                BackCountTextBox.Text = providerSparePartEntity.Count.ToString();
                BackSparePartHiddenField.Value = e.CommandArgument.ToString();
                BackSparePartModalPopupExtender.Show();
            }
            else if (e.CommandName.Equals("LookSparePart"))
            {
                SparePartMultiView.ActiveViewIndex = 1;
                SparePartRecordGridView.DataSource = providerService.GetSparePartRecordModel(Convert.ToInt32(e.CommandArgument));
                SparePartRecordGridView.DataBind();
                NewSparePartPopupExtender.Hide();
                //NewSparePartPanel.Visible = false;
            }
        }

        protected void SparePartGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void SaveBackSparePartButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.BackSparePart(Convert.ToInt32(BackSparePartHiddenField.Value), Convert.ToInt32(BackCountTextBox.Text), ContextService.CurrentAccount);
                BindSparePartGridView();
                BackSparePartMsgLabel.Text = "已成功退回。";
            }
            catch (Exception ex)
            {
                BackSparePartMsgLabel.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void BackSparePartListLnkBtn_Click(object sender, EventArgs e)
        {
            SparePartMultiView.ActiveViewIndex = 0;
        }

        protected void SparePartRecordGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void SparePartRecordGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void NewSparePartButton_Click1(object sender, EventArgs e)
        {
            NewSparePartPopupExtender.Show();
        }

        protected void AddAccessoryButton_Click(object sender, EventArgs e)
        {
            AddAccessoryModalPopupExtender.Show();
        }

        protected void AccessoryListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ProviderService providerService = new ProviderService();
            if (e.CommandName.Equals("BackAccessory"))
            {
                ProviderAccessory accessory = providerService.GetAccessoryByID(Convert.ToInt32(e.CommandArgument));
                BackAccessoryNameTextBox.Text = accessory.ModelNumber;
                BackAccessoryCountTextBox.Text = accessory.Count.ToString();
                AccessoryHiddenField.Value = e.CommandArgument.ToString();
                BackAccessoryModalPopupExtender.Show();
            }
            else
            {
                AccessoryRecordGridView.DataSource = providerService.GetAccessoryRecords(Convert.ToInt32(e.CommandArgument));
                AccessoryRecordGridView.DataBind();

                AccessoryMultiView.ActiveViewIndex = 1;
            }
        }

        protected void AccessoryListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void SaveNewAccessoyButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.AddAccessory(Convert.ToInt32(Request.QueryString["ID"]), NewAccessoryNameTextBox.Text,
                             Convert.ToInt32(NewAccessoryCountTextBox.Text),
                             Convert.ToDecimal(PriceTxtBox.Text.Trim()),
                             Convert.ToDecimal(CustomerPriceTxtBox.Text),
                             ContextService.CurrentAccount);
                BindAccessoryListGridView();
                AddAccessoryMsgLbl.Text = "元器件已成功添加，请继续添加下一条。退出请点击关闭按钮。";                
            }
            catch (Exception ex)
            {
                AddAccessoryMsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
            AddAccessoryModalPopupExtender.Show();
        }

        private void BindAccessoryListGridView()
        {
            ProviderService providerService = new ProviderService();
            AccessoryListGridView.DataSource = providerService.GetAccessoryOfProvider(Convert.ToInt32(Request.QueryString["ID"]));
            AccessoryListGridView.DataBind();
        }

        protected void SaveBackAccessoryButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProviderService providerService = new ProviderService();
                providerService.BackAccessory(Convert.ToInt32(AccessoryHiddenField.Value), Convert.ToInt32(BackAccessoryCountTextBox.Text), ContextService.CurrentAccount);
                BackAccessoryMsgLabel.Text = "元器件已成功退回。";
                BindAccessoryListGridView();
            }
            catch (Exception ex)
            {
                BackAccessoryMsgLabel.Text = string.Format("系统错误:{0}", ex.Message);
                BackAccessoryModalPopupExtender.Show();
            }            
        }

        protected void BackAccessoryListLinkButton_Click(object sender, EventArgs e)
        {
            AccessoryMultiView.ActiveViewIndex = 0;
        }

        protected void AccessoryRecordGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void AccessoryRecordGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ProviderAccessory accrssory = e.Row.DataItem as ProviderAccessory;
                if (accrssory.IsDeleted == true)
                {
                    CheckBox statusChkBox = e.Row.FindControl("statusChkBox") as CheckBox;
                    statusChkBox.Checked = false;
                }
            }
        }

        protected void statusChkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox statusChkBox = sender as CheckBox;
            ProviderService providerService = new ProviderService();
            ProviderAccessory providerAccessoryEntity = providerService.GetAccessoryByID(Convert.ToInt32(statusChkBox.ToolTip));
            providerAccessoryEntity.IsDeleted = !statusChkBox.Checked;
            providerService.UpdateAccessory(providerAccessoryEntity, ContextService.CurrentAccount);
        }
    }
}

